//
//  MDHomeNetTool.swift
//  麦兜直播
//
//  Created by yeqingyu on 16/10/15.
//  Copyright © 2016年 yeqingyu. All rights reserved.
//

import UIKit

class MDHomeNetTool: MDBaseNetTool {
   fileprivate lazy var hotRoomData:MDHomeGroup = MDHomeGroup()//热门
   fileprivate lazy var beautyData:MDHomeGroup = MDHomeGroup()//颜值
//    lazy var HomeDatas:[MDHomeGroup] = [MDHomeGroup]()
    lazy var cycleDatas:[MDCycleScroll] = [MDCycleScroll]()
}



extension MDHomeNetTool {

    // MARK:- 获取热门数据
    func reuestData(result:@escaping()->()) {
        // 1.定义参数
        let parameters = ["limit" : "4", "offset" : "0", "time" : Date.getCurrentTimestamp()]
        ///创建队列
        // 2.创建Group
        let dGroup = DispatchGroup()
        
        // 3.请求第一部分推荐数据
        dGroup.enter()
        
        NetWorkTool.requestNetData(urlString: "http://capi.douyucdn.cn/api/v1/getbigDataRoom", method: .get, parameters: parameters) { (response) in
            
            guard let responseDic = response as? [String:NSObject] else {return}
            guard let responseData = responseDic["data"] as? [[String:Any]] else {return}
            
            
            self.hotRoomData.tag_name = "热门"
            self.hotRoomData.icon_name = "home_header_hot"
            
            for dic in responseData{
                let room = MDRoom(dic: dic as! [String : NSObject])
                self.hotRoomData.rooms.append(room)
            }
            
          dGroup.leave()
        }
        
           //请求颜值数据
        dGroup.enter()
        NetWorkTool.requestNetData(urlString:  "http://capi.douyucdn.cn/api/v1/getVerticalRoom", method: .get, parameters: parameters) { (response) in
            
            guard let responseDic = response as? [String:NSObject] else {return}
            guard let responseData = responseDic["data"] as? [[String:Any]] else {return}
            
            
            self.beautyData.tag_name = "颜值"
            self.beautyData.icon_name = "home_header_phone"
            
            for dic in responseData{
                let room = MDRoom(dic: dic as! [String : NSObject])
                self.beautyData.rooms.append(room)
            }
            
            dGroup.leave()
        }
        
        
        //请求2-12游戏部分的数据
        dGroup.enter()
        NetWorkTool.requestNetData(urlString:"http://capi.douyucdn.cn/api/v1/getHotCate", method: .get, parameters: parameters) { (response) in
//            print(response)
            
            guard let responseDic = response as? [String:NSObject] else {return}
            guard let responseData = responseDic["data"] as? [[String:Any]] else {return}


            for dic in responseData{
                let homeGroup = MDHomeGroup(dic: dic)
                self.baseRoomGroup.append(homeGroup)
            }
            
            dGroup.leave()
        }

        dGroup.notify(queue: .main) {
            self.baseRoomGroup.insert(self.hotRoomData, at: 0)
            self.baseRoomGroup.insert(self.beautyData, at: 1)
            result()
        }
    }
    
    
    
    func requestScrollData(responseCallBack:@escaping()->()) {
        let parame = ["version" : "2.300"]
        
        NetWorkTool.requestNetData(urlString: "http://www.douyutv.com/api/v1/slide/6", method: .get, parameters: parame) { (result) in
            
            guard let resultDic = result as? [String : NSObject] else{
                return
            }
            guard let resultArr = resultDic["data"] as?[[String:NSObject]] else {return}
            
            for dic in resultArr{
            let cyclScroll = MDCycleScroll(dict: dic)
             self.cycleDatas.append(cyclScroll)
            }
            responseCallBack()
        }
    }
}
